/**
 * 
 */
package pl.kaczanowscy.tomek.ipdl.strategy;

import pl.kaczanowscy.tomek.ipdl.RoundResult;
import pl.kaczanowscy.tomek.ipdl.Strategy;
import pl.kaczanowscy.tomek.ipdl.enums.ClassicValue;
import pl.kaczanowscy.tomek.ipdl.enums.Move;

/**
 * Pavlov - win stay, loose shift.
 * 
 * @author Tomek Kaczanowski
 * 
 */
public final class Pavlov implements Strategy {

	private Move lastMove = Move.COOPERATION;

	private boolean success = true;

	// TODO should this be parametrized ?
	private static final int SUCCESS_THRESHOLD = ClassicValue.REWARD.getValue();

	/**
	 * If previous round was successful Pavlov repeats the same move; otherwise
	 * it switches.
	 * 
	 * @see pl.kaczanowscy.tomek.ipdl.Strategy#getMove()
	 */
	@Override
	public Move getMove() {
		if (success) {
			return lastMove;
		} else {
			return (lastMove == Move.COOPERATION) ? Move.DEFECTION
					: Move.COOPERATION;
		}
	}

	/**
	 * Pavlov checks if the outcome was successful.
	 * 
	 * @see pl.kaczanowscy.tomek.ipdl.RoundListener#roundEnded(pl.kaczanowscy.tomek.ipdl.RoundResult)
	 */
	@Override
	public void roundEnded(RoundResult result) {
		lastMove = result.getMove(this);
		success = (result.getPayoff(this) >= SUCCESS_THRESHOLD);
	}

}
